<?php
namespace app\agent\controller;
use think\Controller;
use think\Db;
use think\Request;
use think\Session;

class Editpwd extends Base
{
    public function index()
    {
        return view('editpwd/index');
    }

    public function edit(){
        $param = myarr($this->request->post());
        if($param['newpassword']==null) return "<script>alert('请输入密码');history.go(-1)</script>";
        if($param['newpassword2']==null) return "<script>alert('请输入密码');history.go(-1)</script>";
        if($param['newpassword']!=$param['newpassword2']) return "<script>alert('两次密码不一致');history.go(-1)</script>";
        $agent = Db::name('agents')->where('id',session('agents.id'))->find();
        if(Md5('lc'.$param['oldpassword'])!=$agent['password']) return "<script>alert('旧密码验证失败');history.go(-1)</script>";
        $up = Db::name('agents')->where('id',$agent['id'])->update(['password'=>Md5('lc'.$param['newpassword'])]);
        $data = [
            'wid'=>session('agents.id'),
            'logintoip' => $_SERVER['REMOTE_ADDR'],
            'logintime' => date('Y-m-d H:i:s'),
            'operation' => '修改登录密码',
            'va'=>2,//代理商
        ];
        Db::name('login_va')->insert($data);
        if($up) echo "<script>alert('修改成功，请重新登录');parent.location.reload();</script>";
        Session(null);
    }

    public function setPhone(){
        $agent = Db::name('agents')->where('id',session('agents.id'))->find();
        if(empty($_POST)){
            if($agent['phonenumber']==null|| preg_match('/(^1[3|4|5|6|7|8|9]\d{9}$)|(^09\d{8}$)/',$agent['phonenumber'])!= 1){
                return view('editpwd/setphone');
            }else{
                return view('editpwd/setphone',['agent'=>$agent]);
            }
        }else{
            $param = $this->request->post();
            $user = Db::name('agents')->where('phonenumber',$param['phonenumber'])->find();
            if($user) return "<script>alert('手机号已经被注册');history.go(-1)</script>";
            if($agent['phonenumber']==null|| preg_match('/(^1[3|4|5|6|7|8|9]\d{9}$)|(^09\d{8}$)/',$agent['phonenumber'])!= 1){
                //验证code
                $code = Db::name('user_code')->where(['mobile'=>$param['phonenumber'],'state'=>0])->order('id desc')->find();

                if($code['code']==$param['code']){
                    if(time()>=$code['reg_time']+1800){
                        return "<script>alert('验证码超时');history.go(-1)</script>";
                    }else{
                        $up = Db::name('agents')->where('id',$agent['id'])->update(['phonenumber'=>$param['phonenumber']]);
                        $codeup = Db::name('user_code')->where('id',$code['id'])->update(['state'=>1]);
                        $data = [
                            'wid'=>session('agents.id'),
                            'logintoip' => $_SERVER['REMOTE_ADDR'],
                            'logintime' => date('Y-m-d H:i:s'),
                            'operation' => '添加手机号',
                            'va'=>2,//代理商
                        ];
                        Db::name('login_va')->insert($data);
                        if($up){
                            return $this->success('手机号添加成功','Editpwd/setPhone');
                        }
                    }
                }else{
                    return "<script>alert('验证码验证错误');history.go(-1)</script>";
                }
            }
        }

    }

    //注册获取短信验证码
    public function registersendMsg(){
        $mobile = $_POST['mobile'];
        $agent = Db::name('agents')->where('phonenumber',$mobile)->find();
        if($agent) return ['msg'=>'手机号已经被人注册'];
        //一分钟只能发生一次
        $usercode = Db::name('user_code')->where('mobile',$mobile)->order('id desc')->find();
        if($usercode['reg_time']+60 >= time()) return ['msg'=>'一分钟只能发送一次'];
        $tplParam = rand(1000,9999);
        $tpCode = 'SMS_134815251';//支付密码添加
        $message = sendMsg($mobile,$tpCode,$tplParam);
        if($message['Message']=='OK'){
            $data = [
                'mobile' => $mobile,
                'code'   => $tplParam,
                'reg_time'=> time(),
            ];
            $code = Db::name('user_code')->insert($data);
            if($code){
                return ['msg'=>'success'];
            }else{
                return ['msg'=>'验证码发送错误'];
            }
        }else{
            return ['msg'=>'验证码发送错误'];
        }

    }

}
